package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.ActionSettingsEntity;
import io.renren.modules.app.service.ActionSettingsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 系统操作设置表
 *
 * @date 2022-04-20 22:37:45
 */
@RestController
@RequestMapping("app/actionsettings")
public class ActionSettingsController {
    @Autowired
    private ActionSettingsService actionSettingsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:actionsettings:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = actionSettingsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{settingsId}")
    @RequiresPermissions("generator:actionsettings:info")
    public R info(@PathVariable("settingsId") Long settingsId) {
        ActionSettingsEntity actionSettings = actionSettingsService.getById(settingsId);

        return R.ok().put("actionSettings", actionSettings);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:actionsettings:save")
    public R save(@RequestBody ActionSettingsEntity actionSettings) {
        actionSettings.setCreateTime(new Date());
        actionSettingsService.save(actionSettings);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:actionsettings:update")
    public R update(@RequestBody ActionSettingsEntity actionSettings) {
        actionSettingsService.updateById(actionSettings);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:actionsettings:delete")
    public R delete(@RequestBody Long[] settingsIds) {
        actionSettingsService.removeByIds(Arrays.asList(settingsIds));

        return R.ok();
    }

    /**
     * 查询平台交易设置
     *
     * @return
     */
    @Login
    @GetMapping("getActionSettings")
    public R getActionSettings() {
        ActionSettingsEntity actionSettings = actionSettingsService.getActionSettings();
        return R.ok().put("data", actionSettings);
    }

    /**
     * 检验充值、提现、抢单时间
     *
     * @param type 检验类型
     * @return
     */
    @Login
    @GetMapping("testOperate")
    public R testOperate(Integer type) {
        if (StringUtils.isNull(type)) {
            return R.error("类型不能为空");
        }
        Calendar now = Calendar.getInstance();
        //当前时间(时)
        int currentTime = now.get(Calendar.HOUR_OF_DAY);
        ActionSettingsEntity actionSettings = actionSettingsService.getActionSettings();
        //检验充值
        if (type.equals(0)) {
            if (currentTime >= actionSettings.getRechargeBeginTime() && currentTime <= actionSettings.getRechargeEndTime()) {
                return R.ok();
            } else {
                return R.error("禁止在" + actionSettings.getRechargeBeginTime() + ":00" + " - " + actionSettings.getRechargeEndTime() + ":00" + "以外的时间段充值");
            }
            //检验提现
        } else if (type.equals(1)) {
            if (currentTime >= actionSettings.getWithdrawalsBeginTime() && currentTime <= actionSettings.getWithdrawalsEndTime()) {
                return R.ok();
            } else {
                return R.error("禁止在" + actionSettings.getWithdrawalsBeginTime() + ":00" + " - " + actionSettings.getWithdrawalsEndTime() + ":00" + "以外的时间段提现");
            }
            //检验抢单
        } else if (type.equals(2)) {
            if (currentTime >= actionSettings.getGrabBeginTime() && currentTime <= actionSettings.getGrabEndTime()) {
                return R.ok();
            } else {
                return R.error("禁止在" + actionSettings.getGrabBeginTime() + ":00" + "-" + actionSettings.getGrabEndTime() + ":00" + "以外的时间段抢票");
            }
        }
        return R.error();
    }


}
